/*
 * Creation : 24/09/2014
 */
package com.mycompany.app.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.mycompany.app.model.Customer;
import com.mycompany.app.service.CustomerService;
import com.opensymphony.xwork2.ModelDriven;

public class CustomerAction implements ModelDriven {

	Customer customer = new Customer();
	List<Customer> customerList = new ArrayList<Customer>();

	CustomerService customerService;

	// DI via Spring
	public void setCustomerBo(CustomerService customerService) {
		this.customerService = customerService;
	}

	public Object getModel() {
		return customer;
	}

	public List<Customer> getCustomerList() {
		return customerList;
	}

	public void setCustomerList(List<Customer> customerList) {
		this.customerList = customerList;
	}

	// save customer
	public String addCustomer() throws Exception {

		// save it
		customer.setCreatedDate(new Date());
		customerService.addCustomer(customer);

		// reload the customer list
		customerList = null;
		customerList = customerService.listCustomer();

		return "success";

	}

	// list all customers
	public String listCustomer() throws Exception {

		customerList = customerService.listCustomer();

		return "success";

	}

}
